﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Scavenger
{
    class DrawPrimitives // This class should not be used in the actual game, it is only for easy drawing of helping shapes.
    {
        private Vector2 Centroid, Size;
        private float Width, Height, Rotation;
        private String Type;
        public int moveSpeed = 2;
        public const float MeterInPixels = 64f;
        public BasicPrimitives Texture;

        public void Initialize(String type, Vector2 position,
                                Vector2 size, BasicPrimitives texture)
        {
            Type = type;
            Size = size;
            Width = Size.X * 2;
            Height = Size.Y * 2;
            Centroid = new Vector2(position.X, position.Y);
            Texture = texture;
            SetTextureProperties();
        }

        public void Initialize(VectorStuff.Line line, BasicPrimitives texture)
        {
            Type = "Line";
            Texture = texture;
            Vector2[] positions = Stretch(line);
            Texture.CreateLine(positions[0], positions[1]);
        }

        public void Initialize(VectorStuff.Vector vector, BasicPrimitives texture)
        {
            Type = "Vector";
            Texture = texture;
            Texture.CreateLine(vector.PointStart, vector.PointEnd);
        }

        private void SetTextureProperties()
        {
            if (Type == "Circle")
            {
                Texture.CreateEllipse(Size.X, Size.Y, 64);
                Texture.Position = Centroid;
            }
            else if (Type == "Rectangle")
            {
                Texture.CreateSquare(-Size, Size);
                Texture.Position = Centroid;
            }
        }

        public void UpdateTexture(VectorStuff.Line line)
        {
            Vector2[] positions = Stretch(line);
            Texture.CreateLine(positions[0], positions[1]);
        }

        public void UpdateTexture(VectorStuff.Vector vector)
        {
            Texture.CreateLine(vector.PointStart, vector.PointEnd);
        }

        private Vector2[] Stretch(VectorStuff.Line line)
        {
            float i = 0;
            Vector2[] positions = new Vector2[2];
            do
            {
                positions[0] = line.PointStart + line.DirectionVector * i * 10;
                i++;
            } while (positions[0].X > 0 && positions[0].Y > 0 &&
                        positions[0].Y < 480 && positions[0].X < 800);
            do
            {
                positions[1] = line.PointStart - line.DirectionVector * i * 10;
                i++;
            } while (positions[1].X > 0 && positions[1].Y > 0 &&
                        positions[1].Y < 480 && positions[1].X < 800);

            return positions;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Texture.RenderPolygonPrimitive(spriteBatch, Rotation, Vector2.Zero);
        }
    }
}
